//
//  YDOperation.m
//  YDBASE
//
//  Created by rowena on 2019/5/15.
//  Copyright © 2019 wuyezhiguhun. All rights reserved.
//

#import "YDOperation.h"


@interface YDOperation()

@property (nonatomic, strong) NSInvocationOperation *invocationOperation;

@end

@implementation YDOperation


- (instancetype)init {
    self = [super init];
    if (self) {
        
        self.invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(invocationOperationTarget) object:nil];
        
    }
    return self;
}

- (void)invocationOperationTarget {
    
    NSLog(@"invocationOperationTarget ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    
}

/**
 * NSBlockOperation
 */
- (void)blockOperation1 {
    
    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"blockOperation1 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operation start];
}

/**
 * NSBlockOperation队列
 */
- (void)blockOperationWithExecutionList {
    
    NSBlockOperation *operation = [[NSBlockOperation alloc] init];
    
    [operation addExecutionBlock:^{
        NSLog(@"blockOperationWithExecutionList ==>:1 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operation addExecutionBlock:^{
        NSLog(@"blockOperationWithExecutionList ==>:2 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operation addExecutionBlock:^{
        NSLog(@"blockOperationWithExecutionList ==>:3 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operation addExecutionBlock:^{
        NSLog(@"blockOperationWithExecutionList ==>:4 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operation addExecutionBlock:^{
        NSLog(@"blockOperationWithExecutionList ==>:5 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    [operationQueue addOperation:operation];
    
}

/**
 * 使用NSOperationQueue来创建任务
 */
- (void)operationQueueWithOperationList {
    
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    
    [operationQueue addOperationWithBlock:^{
        
        NSLog(@"operationQueueWithOperationList ==>:1 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
        
    }];
    
    [operationQueue addOperationWithBlock:^{
        
        NSLog(@"operationQueueWithOperationList ==>:2 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
        
    }];
    
    [operationQueue addOperationWithBlock:^{
        
        NSLog(@"operationQueueWithOperationList ==>:3 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
        
    }];
    
    [operationQueue addOperationWithBlock:^{
        
        NSLog(@"operationQueueWithOperationList ==>:4 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
        
    }];
    
    [operationQueue addOperationWithBlock:^{
        
        NSLog(@"operationQueueWithOperationList ==>:5 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
        
    }];
    
}

/**
 * 队列的使用----依赖执行任务
 */
- (void)depenceyOperation {
    
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"depenceyOperation ==>:1 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"depenceyOperation ==>:2 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"depenceyOperation ==>:3 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    NSBlockOperation *operation4 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"depenceyOperation ==>:4 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    NSBlockOperation *operation5 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"depenceyOperation ==>:5 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    NSBlockOperation *operation6 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"depenceyOperation ==>:6 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operation6 addDependency:operation5];
    [operation5 addDependency:operation4];
    [operation4 addDependency:operation3];
    [operation3 addDependency:operation2];
    [operation2 addDependency:operation1];
    
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    
    [operationQueue addOperation:operation1];
    [operationQueue addOperation:operation2];
    [operationQueue addOperation:operation3];
    [operationQueue addOperation:operation4];
    [operationQueue addOperation:operation5];
    [operationQueue addOperation:operation6];
    
    
}

/**
 * 队列的使用---设置最大并发数
 */
- (void)maxConcurrentOperationCount {
    
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    operationQueue.maxConcurrentOperationCount = 3;
    
    [operationQueue addOperationWithBlock:^{
        NSLog(@"macConcurrentOperationCount ==>:1 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operationQueue addOperationWithBlock:^{
        NSLog(@"macConcurrentOperationCount ==>:2 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operationQueue addOperationWithBlock:^{
        NSLog(@"macConcurrentOperationCount ==>:3 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operationQueue addOperationWithBlock:^{
        NSLog(@"macConcurrentOperationCount ==>:4 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operationQueue addOperationWithBlock:^{
        NSLog(@"macConcurrentOperationCount ==>:5 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operationQueue addOperationWithBlock:^{
        NSLog(@"macConcurrentOperationCount ==>:6 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operationQueue addOperationWithBlock:^{
        NSLog(@"macConcurrentOperationCount ==>:7 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operationQueue addOperationWithBlock:^{
        NSLog(@"macConcurrentOperationCount ==>:8 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
    [operationQueue addOperationWithBlock:^{
        NSLog(@"macConcurrentOperationCount ==>:9 ==>: CurrentThread ==>: %@ \n", [NSThread currentThread]);
    }];
    
}





@end























